package com.lzy.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.lzy.annotation.AuthCheck;
import com.lzy.annotation.Log;
import com.lzy.common.R;
import com.lzy.enums.BusinessType;
import com.lzy.vo.AuthorDeptVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.lzy.entity.SysRoleEntity;
import com.lzy.service.ISysRoleService;

/**
 * 角色信息表
 *
 * @author lyw
 * @email lyw@gmail.com
 * @date 2023-02-02 17:06:20
 */
@Api(tags = "角色模块")
@RestController
@RequestMapping("/sysrole")
public class SysRoleController {
    @Autowired
    private ISysRoleService iSysRoleService;

    /**
     * 列表
     */
    @PostMapping("/list")
    @ApiOperation(value = "角色列表")
    @AuthCheck(hasAuthority = "system:role:list")
    public R list(@RequestBody SysRoleEntity sysRoleEntity){

        List<SysRoleEntity> list = iSysRoleService.selectAllRolesByConditional(sysRoleEntity);
        return R.ok().put("records", list);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{roleId}")
    @ApiOperation(value = "角色信息")
    @AuthCheck(hasAuthority = "system:role:query")
    public R info(@ApiParam("角色id") @PathVariable("roleId") Long roleId){

        SysRoleEntity sysRole = iSysRoleService.selectRoleInfoByRoleId(roleId);
        return R.ok().put("sysRole", sysRole);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation(value = "保存角色")
    @AuthCheck(hasAuthority = "system:role:add")
    @Log(title = "角色模块", businessType = BusinessType.INSERT)
    public R save(@RequestBody SysRoleEntity sysRole){

        iSysRoleService.saveNewRoleInfo(sysRole);
        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation(value = "更新角色")
    @AuthCheck(hasAuthority = "system:role:edit")
    @Log(title = "角色模块", businessType = BusinessType.UPDATE)
    public R update(@Validated @RequestBody SysRoleEntity sysRole){

        iSysRoleService.updateRoleInfo(sysRole);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除角色")
    @AuthCheck(hasAuthority = "system:role:remove")
    @Log(title = "角色模块", businessType = BusinessType.DELETE)
    public R delete(@RequestBody Long[] roleIds){

        iSysRoleService.deleteRole(Arrays.asList(roleIds));
        return R.ok();
    }

    /**
     * 授权部门
     */
    @ApiOperation("授权部门")
    @PostMapping("/authorDept")
    @AuthCheck(hasAuthority = "system:role:edit")
    @Log(title = "角色授权部门", businessType = BusinessType.INSERT)
    public R authorDept(@RequestBody AuthorDeptVo authorDeptVo) {

        iSysRoleService.authorDept(authorDeptVo);
        return R.ok();
    }
}
